﻿using System;
using System.Collections.Generic;
using BO.Infrastructure.Utils;
using ChannelVN.Cached;
using Utilities.Entities;

namespace BO.ProjectManager
{
    public class PMBO
    {
        static readonly string UniqueText = typeof(PMBO).Namespace + typeof(PMBO).Name;

        static string BuildCacheKey(string methodName, IEnumerable<object> listParams)
        {
            return PresentationUtils.GetCacheKey(methodName, listParams, UniqueText);
        }

        public static List<ExtensionSourceSet> GetExtensionSourceSets()
        {
            var cacheKey = BuildCacheKey("GetExtensionSourceSets", null);
            var result = CacheController.GetCacheDependencyInstance().Get<List<ExtensionSourceSet>>(cacheKey);
            if (result == null)
            {
                string fileDependency;
                result = PMBO_NoCache.GetExtensionSourceSets(out fileDependency);
                if (result != null)
                {
                    CacheController.GetCacheDependencyInstance().Add(fileDependency, cacheKey, result, DateTime.Now.AddDays(1));
                }
            }
            return result;
        }

        public static StorageConfig GetCurrentStorageConfig()
        {
            var cacheKey = BuildCacheKey("GetCurrentStorageConfig", null);
            var result = CacheController.GetCacheDependencyInstance().Get<StorageConfig>(cacheKey);
            if (result == null)
            {
                string fileDependency;
                result = PMBO_NoCache.GetCurrentStorageConfig(out fileDependency);
                if (result != null)
                {
                    CacheController.GetCacheDependencyInstance().Add(fileDependency, cacheKey, result, DateTime.Now.AddDays(1));
                }
            }
            return result ?? new StorageConfig
                {
                    DefaultVersion = 1,
                    Domain = "http://soha4.vcmedia.vn/",
                    IsUsed = false
                };
        }
    }
}
